﻿$(function () {
    jQuery.ajaxSetup({
        beforeSend: function () {
            $('#loader').show();
        },
        complete: function () {
            $('#loader').hide()
        },
        success: function () { }
    });
});
function capnhattaikhoan() {
    $.ajax({
        type: "POST",
        url: "/TaiKhoan/CapNhatThongTin",
        data: $("#form-cap-nhat").serialize(),
        dataType: "text",
        success: function (data) {
            $("#ket_qua").show();
            $("#ket_qua").text(data);
            $("#ket_qua").fadeOut(4000);
        }
    });
}

function postComment() {
    $.ajax({
        type: "POST",
        url: "/Comment/DangComment",
        data: $("#form-dang-comment").serialize(),
        dataType: "html",
        success: function (data) {            
            $("#sp_comment").prepend(data);
            $('#sp_comment').fadeIn('fast');
        }

    });
}
function capNhatGridLoaiSP() {

    $.ajax({
        type: "POST",
        url: "/LoaiSanPham/DanhSach",        
        dataType: "html",
        success: function (data) {
            $("#grid-lsp").html(data);
            UpdateGridBinding();
        }

    });
   
}

function ThemLoaiSanPham() {
    $.ajax({
        type: "POST",
        url: "/LoaiSanPham/Them",
        data: $("#form-them-loaisp").serialize(),
        dataType: "text",
        success: function (data) {
            if (data == 'successful') {
                capNhatGridLoaiSP();                
            } else {
                alert("Thêm loại sản phẩm mới thất bại!");
            }

        }
    });
}

function GetCountDown() {
    $.ajax({
        type: "POST",
        url: "/DauGia/GetCountDown",
        dataType: "text",
        success: function (data) {
            var obj = jQuery.parseJSON(data);
            jQuery.each(obj, function (i, val) {
                var ele = ".countdown[MaDauGia='" + val.MaDauGia + "']";
                time = new Date(val.ThoiGianKetThuc.Nam,
                             val.ThoiGianKetThuc.Thang - 1,
                             val.ThoiGianKetThuc.Ngay,
                             val.ThoiGianKetThuc.Gio,
                             val.ThoiGianKetThuc.Phut,
                             val.ThoiGianKetThuc.Giay);

                $(ele).countdown({
                                   labels: ['Năm', 'Tháng', 'Tuần', 'Ngày', 'Giờ', 'Phút', 'Giây'], 
                                   until: time, 
                                   format: 'HMS',
                                   expiryText: '<div class="over">Hết phiên đấu giá</div>',
                                   onTick: highlightLast10
                                });
            });

        }

    });
}

function highlightLast10(periods) {
    if ($.countdown.periodsToSeconds(periods) == 10) {
        $(this).addClass('highlight');
    }
} 

function UpdateCountDown() {
    $.ajax({
        type: "POST",
        url: "/DauGia/GetCountDown",
        dataType: "text",
        success: function (data) {
            var obj = jQuery.parseJSON(data);
            jQuery.each(obj, function (i, val) {
                var timer = ".countdown[MaDauGia='" + val.MaDauGia + "']";
                time = new Date(val.ThoiGianKetThuc.Nam,
                             val.ThoiGianKetThuc.Thang - 1,
                             val.ThoiGianKetThuc.Ngay,
                             val.ThoiGianKetThuc.Gio,
                             val.ThoiGianKetThuc.Phut,
                             val.ThoiGianKetThuc.Giay);
                eleString = ".item_sanpham_dg[MaDauGia='" + val.MaDauGia + "'] table tr.gia_dght td.giahientai-value";
                eleTenTK = ".item_sanpham_dg[MaDauGia='" + val.MaDauGia + "'] table tr.nguoi_dght td.nguoihientai-value";
                var compareValue = $(eleString).text();
                if (compareValue != val.GiaKhopLenh) {
                    $(timer).countdown('change', { until: time });
                    $(timer).hide('fast');
                    $(timer).fadeIn('slow');

                    $(eleString).text(val.GiaKhopLenh);

                    $(eleTenTK).hide('fast');
                    $(eleTenTK).fadeIn('slow');
                    $(eleTenTK).text(val.TaiKhoanCuoi);
                }

            });

        }

    });
}


function XoaLoaiSanPham(idlsp) {
    var request = "MaLoaiSanPham=" + idlsp;
    $.ajax({
        type: "POST",
        url: "/LoaiSanPham/Xoa",
        data: request,
        dataType: "text",
        success: function (data) {
            if (data == 'successful') {
                capNhatGridLoaiSP();                
            } else {
                alert("Xóa thất bại!");
            }

        }
    });
}

function capNhatLoaiSanPham() {
    
    $.ajax({
        type: "POST",
        url: "/LoaiSanPham/CapNhat",
        data: $("#f-cn").serialize(),
        dataType: "text",
        success: function (data) {
            if (data == 'successful') {
                capNhatGridLoaiSP();                
                alert("thanh cong");
            } else {
                alert("that bai");
            }
        }

    });

}

function UpdateGridBinding() {

    //jQueryTableStyling();
    $("a.osx").click(function () {
        var id = $(this).attr('id');
        var ten = $(this).attr('ten');
        PushFormValue(id, ten);

    });

    $("a.del").click(function () {
        var id = $(this).attr('idlsp');
        XoaLoaiSanPham(id);
    });

}
function PushFormValue(id, ten) {
    $("#loaisp").val(id);
    $("#tenmoi").val(ten);

}
function jQueryUIStyling() {
    $('input:button, input:submit').button();
    // Style tables.
    $('.webgrid-wrapper').addClass('ui-grid ui-widget ui-widget-content ui-corner-all');
    $('.webgrid-title').addClass('ui-grid-header ui-widget-header ui-corner-top');
    jQueryTableStyling();
}

//
// Style tables using jQuery UI theme. This function is 
// split out separately so that it can be part of the AJAX
// callback of the WebGrid WebHelper in ASP.NET MVC.
//
function jQueryTableStyling() {
    $('.webgrid').addClass('ui-grid-content ui-widget-content');
    $('.webgrid-header').addClass('ui-state-default');
    $('.webgrid-footer').addClass('ui-grid-footer ui-widget-header ui-corner-bottom ui-helper-clearfix');
}